<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include_once "$root/libs/configTest.php";
/*
 * 1.login and out   (finished)
 * 2.get exceptional date array;(finished)
 * 3 similary to part2, and get duty array.(finished)
 * 4.update person detial
 * 
 */

/**
 * Description of stuff
 *
 * @author markxi
 */
class staff {

    //step 1 part1
    public $DBTemp;
    //changehere
    
    protected $id;
    protected $email; //name refactor to email
    protected $givenName;
    protected $sName;
    protected $pw;
    protected $logState = false;
    
    protected $dbName;
    protected $initial;
    protected $adminState;
    //end of step 1 part1
    //step 2 part1
    protected $willingArray = array();
    protected $unableDateArray = array();
    //end of step 2 part1
    //step3 part1
    protected $dutyDateArray; //variable to contain the final duty in format like {date,active}

    //end of step 3 part1
    //session


    //step1 part2

   
    public function __construct($Email, $PW) {
        //$this->setDBTemp($DB);
        $this->DBTemp = new DBconfigTest();
        $this->checkPW($Email, $PW);
    }
    
    public function getEmail(){
        return $this->email;
    }
    public function setEmail($Email){
        $this->email = $Email;
    }
    public function showEmail(){
        echo $this->email;
    }
   
    public function setInitial($Initial){
        $this->initial = $Initial;
    }
    public function getInitial(){
        return $this->initial;
    }
    public function showInitial(){
        echo $this->initial;
    }
    public function setGName($GName){
        $this->givenName = $GName;
    }
    public function getGName(){
        return $this->givenName;
    }
    public function showGName(){
        echo $this->givenName;
    }


    public function setDBTemp($DB){
        $this->DBTemp = $DB;
    }
    public function getDBTemp(){
        return $this->DBTemp;
    }
    
    public function setsName($sName){
        $this->sName = $sName;
    }

    public function getsName() {
        return $this->sName;
    }
    
    public function showsName(){
        echo $this->sName;
    }
    public function setDBName($dbName){
        $this->dbName = $dbName;
    }
    public function getDBName(){
        return $this->dbName;
    }
    public function showDBName(){
        echo $this->dbName;
    }

    public function setPW($tempPW) {
        $this->pw = $tempPW;
    }
    public function getPW(){
        return $this->pw;
    }

    public function getLoginState() {
        return $this->logState;
    }

    public function login() {
        $this->logState = true;
    }

    public function logout() {
        $this->logState = false;
    }
    
    public function setId($ID){
        $this->id = $ID;
    }
    public function showID(){
        echo $this->id;
    }
    
    public function getID(){
        return $this->id;
    }
    public function setAdminState($state){
        $this->adminState=$state;
    }
    public function getAdminState(){
        return $this->adminState;
    }
    public function showAdminState(){
        echo $this->adminState;
    }

    public function checkPW($email, $pw) {

        $sql = "SELECT user_id,gName,surname,pw,email,rosterDBName,initial,admin FROM `roster_users`.`users` WHERE email = '$email'";

        $this->DBTemp->DB->query($sql);
        echo '<pre>';
        $result = $this->DBTemp->DB->get();
        if (count($result) > 0) {
            //print_r($result);
            //print_r($result[0]['pw']);
            if ($result[0]['pw'] == $pw) {
                $this->login();
                $this->setEmail($result[0]['email']);
                $this->setsName($result[0]['surname']);
                $this->setId($result[0]['user_id']);
                $this->setPW($result[0]['pw']);
                $this->setDBName($result[0]['rosterDBName']);
                $this->setGName($result[0]['gName']);
                $this->setInitial($result[0]['initial']);
                $this->setAdminState($result[0]['admin']);
            } else {
                echo("pw is incorrect<br>");
            }
        } else {
            echo('no such user<br>');
        }
    }

    //end of step1 part2
    //stpe 2 part2
    public function setWillingArray($WillingArray) {
        $this->willingArray = $WillingArray;
    }

    public function getWillingArray() {
        return $this->willingArray;
    }
    public function showWillingArray(){
        echo '<pre>';
        print_r($this->willingArray);
    }
    
    public function initialDutyArray($TempDutyArray){
        $DBName= $this->getDBName();
        $rosterId= $this->getLatestRosterId();
        $id= $this->getID();
        $TempDutyArray1=  serialize($TempDutyArray);
        $sql="INSERT INTO `$DBName`.`staffData`(`roster_id`, `staff_id`, `dutiesArray`) VALUES ($rosterId ,$staffId ,'$TempDutyArray1');";
        $this->DBTemp->DB->query($sql);
        return "Add DutyArray successful";
    }
    public function setUnableArray($UnableArray) {
        $this->unableDateArray = $UnableArray;
    }

    public function getUnableArray() {
        return $this->unableDateArray;
    }
    
    public function showUnableArray(){
        echo '<pre>';
        print_r($this->unableDateArray);//need to arsort the array in time order
    }

    //end of step2 part2
    //step 3 part2
    public function getDutyDateArray() {
        return $this->dutyDateArray;
        //conenction to DB and get the final data.
    }
    
    public function initialStaffData($DutiesArray, $ExceptDayArray){
        $rosterDB = $this->getDBName();
        $DutiesArray1 = serialize($DutiesArray);
        $ExceptDayArray1 = serialize($ExceptDayArray);
        if(isset($rosterDB)){
            $tempSQL = "select max(roster_id) from `$rosterDB`.`roster`";
            //$SQLAddDutyArray = ""
            $this->DBTemp->DB->query($tempSQL);
            $result = $this->DBTemp->DB->get();
            $rosterID = $result[0]['max(roster_id)'];
            
            $initialstaffDataSQL = "INSERT INTO `$rosterDB`.`staffData` (`roster_id`, `staff_id`, `dutiesArray`, `exceptDayArray`) VALUES ('$rosterID', '$this->id', '$DutiesArray1','$ExceptDayArray1')";
            $this->DBTemp->DB->query($initialstaffDataSQL);
            return "initial successful";
        }
    }
    
    public function checkInitial(){
        $DBName = $this->getDBName();
        $Id = $this->getID();
        $sqlLastestRoster="select `roster_id` FROM `$DBName`.`roster` where `roster_id`=(select max(`roster_id`) from `$DBName`.`roster`) ";
        $this->DBTemp->DB->query($sqlLastestRoster);
        $rosterIdArray = $this->DBTemp->DB->get();
        $rosterId = $rosterIdArray[0]['roster_id'];
        
        $sqlStaffLastestRoster = "select * from `$DBName`.`staffData` where `roster_id`=$rosterId and `staff_id`=$Id";
        $this->DBTemp->DB->query($sqlStaffLastestRoster);
        $result=  $this->DBTemp->DB->get();
       
        if(count($result)==0){
            return FALSE;
        }else{
            return true;
        }
        
    }
    
    public function fetchDutyArray(){
        $DBName = $this->getDBName();
        $sql="select `dutyName` from `$DBName`.`duties`";
        $this->DBTemp->DB->query($sql);
        $result = $this->DBTemp->DB->get();
        return $result;
    }
    
    public function fetchSundayArray(){
        $DBName = $this->getDBName();
        $sql="select `Date` FROM `$DBName`.`roster` where `roster_id`=(select max(`roster_id`) from `$DBName`.`roster`) ";
        $this->DBTemp->DB->query($sql);
        $result = $this->DBTemp->DB->get();
        return $result;
    }
    
    public function getLatestRosterId(){
        $DBName = $this->getDBName();
        $sql="select max(roster_id) from `$DBName`.`roster`";
        $this->DBTemp->DB->query($sql);
        $result = $this->DBTemp->DB->get();
        return $result[0]["max(roster_id)"];
    }
}

?>
